//
//  NewSoceityDetailViewController.m
//  Utils
//
//  Created by enghou on 2018/6/26.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "NewSoceityDetailViewController.h"

#import <TZImagePickerController.h>

@interface NewSoceityDetailViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *headImageView;

@property (weak, nonatomic) IBOutlet UITextField *soceityOrgNameField;

@property (weak, nonatomic) IBOutlet UITextField *soceityTypeNameField;
@property(nonatomic,strong)NSArray *soceityTypes;
@property(nonatomic,strong)PropertyDictItemModel *soceityTypeModel;

@property (weak, nonatomic) IBOutlet UITextField *registCardNumField;

@property (weak, nonatomic) IBOutlet UITextField *registOrgCodeField;

@property (weak, nonatomic) IBOutlet UITextField *creditNumberField;

@property (weak, nonatomic) IBOutlet UITextField *workAddressField;

@property (weak, nonatomic) IBOutlet UITextField *homeAddressField;

@property (weak, nonatomic) IBOutlet UITextField *stateField;
@property(nonatomic,strong)PropertyDictItemModel *stateModel;
@property(nonatomic,strong)NSArray *states;

@property (weak, nonatomic) IBOutlet UITextField *moneySourceField;

@property (weak, nonatomic) IBOutlet UITextField *approvalDateField;

@property (weak, nonatomic) IBOutlet UITextField *concernStateField;
@property(nonatomic,strong)PropertyDictItemModel *concernModel;
@property(nonatomic,strong)NSArray *concerns;

@property (weak, nonatomic) IBOutlet UITextField *representativeField;
@property (weak, nonatomic) IBOutlet UITextField *protectorManipulatorField;
@property (weak, nonatomic) IBOutlet UITextField *protectorMobileField;
@property (weak, nonatomic) IBOutlet UITextField *protectorTelField;

@property (weak, nonatomic) IBOutlet UITextField *manipulatorNameField;
@property (weak, nonatomic) IBOutlet UITextField *manipulatorCardTypeField;
@property(nonatomic,strong)PropertyDictItemModel *manipulatorCardModel;
@property(nonatomic,strong)NSArray *manipulatorCardTypes;

@property (weak, nonatomic) IBOutlet UITextField *manipulatorCardNumberField;
@property (weak, nonatomic) IBOutlet UITextField *manipulatorMobileField;
@property (weak, nonatomic) IBOutlet UITextField *manipulatorTelField;

@property(nonatomic,strong)UIDatePicker *datePicker;

@end

@implementation NewSoceityDetailViewController

- (IBAction)havingFemaleOrgAction:(UIButton *)sender {
    sender.selected = YES;
    self.notHavingFemaleOrgBtn.selected = NO;
    [self.tableView reloadData];
}

- (IBAction)notHavingFemaleOrgAction:(UIButton *)sender {
    sender.selected = YES;
    self.havingFemaleOrgBtn.selected = NO;
    [self.tableView reloadData];
}

- (IBAction)havingYoungthOrgAction:(UIButton *)sender {
    sender.selected = YES;
    self.notHavingYoungthOrgBtn.selected = NO;
    [self.tableView reloadData];
}


- (IBAction)notHavingYoungthOrgAction:(UIButton *)sender {
    sender.selected = YES;
    self.havingYoungthOrgBtn.selected = NO;
    [self.tableView reloadData];
}

- (IBAction)havingWorkingOrgAction:(UIButton *)sender {
    sender.selected = YES;
    self.notHavingWorkingOrgBtn.selected = NO;
    [self.tableView reloadData];
}
- (IBAction)notHavingWorkingOrgAction:(UIButton *)sender {
    sender.selected = YES;
    self.havingWorkingOrgBtn.selected = NO;
    [self.tableView reloadData];
}

- (IBAction)havingOrgAction:(UIButton *)sender {
    sender.selected = YES;
    self.notHavingOrgBtn.selected = NO;
    [self.tableView reloadData];
}
- (IBAction)notHavingOrgAction:(UIButton *)sender {
    sender.selected = YES;
    self.havingOrgBtn.selected = NO;
    [self.tableView reloadData];
}

- (IBAction)canBiuildDAction:(UIButton *)sender {
    sender.selected = YES;
    self.cantBuildDBtn.selected = NO;
    [self.tableView reloadData];
}

- (IBAction)cantBuildDAction:(UIButton *)sender {
    sender.selected = YES;
    self.canBuildDBtn.selected = NO;
    [self.tableView reloadData];
}

-(instancetype)init{
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Organization" bundle:[NSBundle mainBundle]];
    return [story instantiateViewControllerWithIdentifier:@"NewSoceityDetailViewController"];
}

- (IBAction)showApprovalDateAction:(id)sender {
    [self.approvalDateField becomeFirstResponder];
}

-(void)sureAction{
    [self.view endEditing:YES];
    self.approvalDateField.text = [[self.datePicker date]dateLineStrOnlyDay];
}

-(void)cancelAction{
    [self.view endEditing:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 220)];
    self.datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    self.datePicker.backgroundColor = [UIColor whiteColor];
    self.datePicker.datePickerMode = UIDatePickerModeDate;
    self.datePicker.date = [NSDate date];
    self.approvalDateField.inputView = self.datePicker;
    self.approvalDateField.inputAccessoryView = [self inputAccessView];
    PropertyDictItemModel *model = [PropertyDictItemModel new];
    model.ids = @"0";
    model.displayName = @"不存在";
    PropertyDictItemModel *model1 = [PropertyDictItemModel new];
    model1.ids = @"1";
    model1.displayName = @"存在";
    self.states = @[model,model1];
    self.stateModel = self.states[0];
}

-(void)uploadFiles:(NSArray <NSData *>*)files successBlock:(void(^)(NSArray <FileItemModel *>*))suc{
    [self beginLoading];
    [[NetWorkerManager sharedInstance]uploadImageWithPath:kUploadFilePath parameter:nil files:files fileName:@"file" suffix:@"jpg" completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source listItemClass:[FileItemModel class]];
        if (model.success) {
            if (suc) {
                suc(model.list);
            }
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

#pragma mark - 获取详情
-(NSString *)detailPath{
    return @"/mobile/comprehensiveGovernance/newSocietyOrganizations/getNewSocietyOrganizationsById.action";
}

-(NSDictionary *)detailParam{
    return @{@"id":self.ids};
}

-(void(^)(NSDictionary *))configView{
    void(^config)(NSDictionary *source) = ^(NSDictionary *source){
        NSDictionary *data = source[@"data"];
        self.workAddressField.text = data[@"address"];
        self.approvalDateField.text = data[@"approvalDate"];
        self.concernModel = [PropertyDictItemModel new];
        self.concernModel.ids = data[@"attentionExtent"][@"id"];
        self.concernStateField.text = data[@"attentionExtent"][@"displayName"];
        self.manipulatorCardNumberField.text = data[@"cardNo"];
        self.manipulatorCardModel = [PropertyDictItemModel new];
        self.manipulatorCardModel.ids = data[@"cardType"][@"id"];
        self.manipulatorCardTypeField.text = data[@"cardType"][@"displayName"];
        self.havingYoungthOrgBtn.selected = [data[@"isHaveGroup"] boolValue];
        self.notHavingYoungthOrgBtn.selected = !self.havingYoungthOrgBtn.selected;
        self.havingWorkingOrgBtn.selected = [data[@"isHaveGuild"] boolValue];
        self.notHavingWorkingOrgBtn.selected = !self.havingWorkingOrgBtn.selected;
        self.havingOrgBtn.selected = [data[@"isHaveOrganization"] boolValue];
        self.notHavingOrgBtn.selected = !self.havingOrgBtn.selected;
        self.canBuildDBtn.selected = [data[@"isHaveOverSea"] boolValue];
        self.cantBuildDBtn.selected = !self.canBuildDBtn.selected;
        self.havingFemaleOrgBtn.selected = [data[@"isHaveWomenFederation"] boolValue];
        self.notHavingFemaleOrgBtn.selected = !self.havingFemaleOrgBtn.selected;
        self.manipulatorNameField.text = data[@"legalPerson"];
        self.representativeField.text = data[@"legalPersonName"];
        self.manipulatorTelField.text = data[@"legalPersonTelephone"];
        self.protectorManipulatorField.text = data[@"legalSafePerson"];
        self.protectorTelField.text = data[@"legalSafePersonTelephone"];
        self.moneySourceField.text = data[@"moneySource"];
        self.soceityOrgNameField.text = data[@"name"];
        self.soceityTypeModel = [PropertyDictItemModel new];
        self.soceityTypeModel.ids = data[@"type"][@"id"];
        self.soceityTypeNameField.text = data[@"type"][@"displayName"];
        self.dangyuanCountField.text = [NSString stringWithFormat:@"%li",[data[@"partyNum"] integerValue]];
        self.registCardNumField.text = data[@"registrationNumbers"];
        self.homeAddressField.text = data[@"residence"];
        self.stateField.text = nil;
        self.dangMemberCountField.text = [NSString stringWithFormat:@"%li",[data[@"theMemeberOfGroup"] integerValue]];
        self.gonghuiMemberCountField.text = [NSString stringWithFormat:@"%li",[data[@"theMemeberOfGuild"] integerValue]];
        self.femaleCountField.text = [NSString stringWithFormat:@"%li",[data[@"theMemeberOfWomenFederation"] integerValue]];
        self.creditNumberField.text = data[@"unifiedSocialCreditCode"];
        self.registOrgCodeField.text = data
        [@"registrationManagementCode"];
        self.stateModel.ids = [NSString stringWithFormat:@"%li",[data[@"state"] integerValue]];
        if ([self.stateModel.ids isEqualToString:@"0"]) {
            self.stateField.text = @"不存在";
        }else{
            self.stateField.text = @"存在";
        }
        [self.tableView reloadData];
    };
    return config;
}

#pragma 更新记录
-(NSString *)updatePath{
    return @"/mobile/comprehensiveGovernance/newSocietyOrganizations/updateNewSocietyOrganizations.action";
}

-(NSDictionary *)updateParameter{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if (self.ids) {
        param[@"newSocietyOrganizations.id"] = self.ids;
    }
    NSString *orgId = [[LocalPersistent sharedInstance]contentForKey:@"orgId"];
    if (orgId) {
        param[@"orgId"] = orgId;
    }
    if (self.workAddressField.text.length > 0) {
        param[@"newSocietyOrganizations.address"] = self.workAddressField.text;
    }else{
        [self alertSome:@"请输入办公地址!"];
        return nil;
    }
    if (self.homeAddressField.text.length > 0) {
        param[@"newSocietyOrganizations.residence"] = self.homeAddressField.text;
    }else{
        [self alertSome:@"请输入住所地址!"];
        return nil;
    }
    if (self.soceityOrgNameField.text.length > 0) {
        param[@"newSocietyOrganizations.name"] = self.soceityOrgNameField.text;
    }else{
        [self alertSome:@"请输入社会组织名称!"];
        return nil;
    }
    if (self.soceityTypeModel.ids.length > 0) {
        param[@"newSocietyOrganizations.type.id"] = self.soceityTypeModel.ids;
    }else{
        [self alertSome:@"请选择组织类型"];
        return nil;
    }
    if (self.registCardNumField.text.length > 0) {
        param[@"newSocietyOrganizations.registrationNumbers"] = self.registCardNumField.text;
    }else{
        [self alertSome:@"请输入登记证号"];
        return nil;
    }
    if (self.stateModel.ids.length > 0) {
        param[@"newSocietyOrganizations.state"] = self.stateModel.ids;
    }else{
        [self alertSome:@"请选择状态"];
        return nil;
    }
    if (self.moneySourceField.text.length > 0) {
        param[@"newSocietyOrganizations.moneySource"] = self.moneySourceField.text;
    }else{
        [self alertSome:@"请输入资金来源"];
        return nil;
    }
    if (self.approvalDateField.text.length > 0) {
        param[@"newSocietyOrganizations.approvalDate"] = self.approvalDateField.text;
    }else{
        [self alertSome:@"请选择许可日期"];
        return nil;
    }
    if (self.concernModel.ids.length > 0) {
        param[@"newSocietyOrganizations.attentionExtent.id"] = self.concernModel.ids;
    }else{
        [self alertSome:@"请选择关注程度"];
        return nil;
    }
    if (self.representativeField.text.length > 0) {
        param[@"newSocietyOrganizations.legalPersonName"] = self.representativeField.text;
    }else{
        [self alertSome:@"请输入法人姓名"];
        return nil;
    }
    if (self.protectorManipulatorField.text.length > 0) {
        param[@"newSocietyOrganizations.legalSafePerson"] = self.protectorManipulatorField.text;
    }else{
        [self alertSome:@"请输入治保负责人姓名"];
        return nil;
    }
    if (self.manipulatorNameField.text.length > 0) {
        param[@"newSocietyOrganizations.legalPerson"] = self.manipulatorNameField.text;
    }else{
        [self alertSome:@"请输入负责人姓名"];
        return nil;
    }
    if (self.manipulatorCardModel.ids.length > 0) {
        param[@"newSocietyOrganizations.cardType.id"] = self.manipulatorCardModel.ids;
    }else{
        [self alertSome:@"请选择证件类型"];
        return nil;
    }
    if (self.manipulatorCardNumberField.text.length > 0) {
        param[@"newSocietyOrganizations.cardNo"] = self.manipulatorCardNumberField.text;
    }else{
        [self alertSome:@"请输入负责人证件号"];
        return nil;
    }
    if (self.registOrgCodeField.text.length > 0) {
        param[@"newSocietyOrganizations.registrationManagementCode"] = self.registOrgCodeField.text;
    }else{
        [self alertSome:@"请输入登记管理机构代码"];
        return nil;
    }
    if (self.creditNumberField.text.length > 0) {
        param[@"newSocietyOrganizations.unifiedSocialCreditCode"] = self.creditNumberField.text;
    }else{
        [self alertSome:@"请输入统一社会信用代码"];
        return nil;
    }
    if (self.manipulatorMobileField.text.length > 0) {
        param[@"newSocietyOrganizations.legalPersonMobileNumber"] = self.manipulatorMobileField.text;
    }else{
        [self alertSome:@"请输入负责人手机号"];
        return nil;
    }
    if (self.manipulatorTelField.text.length > 0) {
        param[@"newSocietyOrganizations.legalPersonTelephone"] = self.manipulatorTelField.text;
    }else{
        [self alertSome:@"请输入负责人座机"];
        return nil;
    }
    if (self.protectorMobileField.text.length > 0) {
        param[@"newSocietyOrganizations.legalSafePersonMobileNumber"] = self.protectorMobileField.text;
    }else{
        [self alertSome:@"请输入治保负责人手机号"];
        return nil;
    }
    
    if (self.protectorTelField.text.length > 0) {
        param[@"newSocietyOrganizations.legalSafePersonTelephone"] = self.protectorTelField.text;
    }else{
        [self alertSome:@"请输入治保负责人座机"];
        return nil;
    }
    
    if (self.canBuildDBtn.selected) {
        param[@"newSocietyOrganizations.isHaveOverSea"] = @"true";
    }else{
        param[@"newSocietyOrganizations.isHaveOverSea"] = @"false";
    }
    
    if (self.havingOrgBtn.selected) {
        param[@"newSocietyOrganizations.isHaveOrganization"] = @"true";
        if (self.dangyuanCountField.text.length > 0) {
            param[@"newSocietyOrganizations.partyNum"] = self.dangyuanCountField.text;
        }else{
            [self alertSome:@"请输入党员数"];
            return nil;
        }
    }else{
        param[@"newSocietyOrganizations.isHaveOrganization"] = @"false";
    }
    
    if (self.havingWorkingOrgBtn.selected) {
        param[@"newSocietyOrganizations.isHaveGuild"] = @"true";
        if (self.gonghuiMemberCountField.text.length > 0) {
            param[@"newSocietyOrganizations.theMemeberOfGuild"] = self.gonghuiMemberCountField.text;
        }else{
            [self alertSome:@"请输入工会会员数"];
            return nil;
        }
    }else{
        param[@"newSocietyOrganizations.isHaveGuild"] = @"false";
    }
    
    if (self.havingYoungthOrgBtn.selected) {
        param[@"newSocietyOrganizations.isHaveGroup"] = @"true";
        if (self.dangMemberCountField.text.length > 0) {
            param[@"newSocietyOrganizations.theMemeberOfGroup"] = self.dangMemberCountField.text;
        }else{
            [self alertSome:@"请输入共青团员数"];
            return nil;
        }
    }else{
        param[@"newSocietyOrganizations.isHaveGroup"] = @"false";
    }
    
    if (self.havingFemaleOrgBtn.selected) {
        param[@"newSocietyOrganizations.isHaveWomenFederation"] = @"true";
        if (self.femaleCountField.text.length > 0) {
            param[@"newSocietyOrganizations.theMemeberOfWomenFederation"] = self.femaleCountField.text;
        }else{
            [self alertSome:@"请输入妇女数量"];
            return nil;
        }
    }else{
        param[@"newSocietyOrganizations.isHaveWomenFederation"] = @"false";
    }
    if (self.filePath) {
        param[@"file.path"] = self.filePath;
    }
    return param;
}

-(void)updateRecord{
    NSDictionary *param = [self updateParameter];
    if (!param) {
        return;
    }
    if (self.headImageView.image) {
        NSData *data = UIImageJPEGRepresentation(self.headImageView.image, 0.1);
        [self uploadFiles:@[data] successBlock:^(NSArray<FileItemModel *> *files) {
            FileItemModel *model = files[0];
            self.filePath = model.path;
            [super updateRecord];
        }];
    }else{
        [super updateRecord];
    }
    
}

//新增记录
-(NSString *)addPath{
    return @"/mobile/comprehensiveGovernance/newSocietyOrganizations/addNewSocietyOrganizations.action";
}

-(NSDictionary *)addedParameter{
    return [self updateParameter];
}

-(void)addRecord{
    NSDictionary *param = [self updateParameter];
    if (!param) {
        return;
    }
    if (self.headImageView.image) {
        NSData *data = UIImageJPEGRepresentation(self.headImageView.image, 0.1);
        [self uploadFiles:@[data] successBlock:^(NSArray<FileItemModel *> *files) {
            FileItemModel *model = files[0];
            self.filePath = model.path;
            [super addRecord];
        }];
    }else{
        [super addRecord];
    }
}

//删除记录
-(NSString *)delPath{
    return @"/mobile/comprehensiveGovernance/newSocietyOrganizations/deleteNewSocietyOrganizationsById.action";
}

-(NSDictionary *)delParameter{
    return @{@"id":self.ids};
}

//操作完成后发送的通知
-(NSString *)refresh_not_name{
    return @"refresh_new_soceity";
}

-(void)pickerImage{
    TZImagePickerController *picker = [[TZImagePickerController alloc]initWithMaxImagesCount:1 delegate:nil];
    picker.didFinishPickingPhotosWithInfosHandle = ^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto, NSArray<NSDictionary *> *infos) {
        self.headImageView.image = photos[0];
    };
    [self presentVC:picker];
}

-(void)chooseOrgType{
    [self commonAlertWithContent:@"社会组织类型" choosenBlock:^(PropertyDictItemModel *model) {
        self.soceityTypeModel = model;
        self.soceityTypeNameField.text = model.displayName;
    } getList:^(NSArray *list) {
        self.soceityTypes = list;
    }];
}

-(void)chooseManipulatorCardType{
    
    [self commonAlertWithContent:@"证件代码" choosenBlock:^(PropertyDictItemModel *model) {
        self.manipulatorCardModel = model;
        self.manipulatorCardTypeField.text = model.displayName;
    } getList:^(NSArray *list) {
        self.manipulatorCardTypes = list;
    }];
    
}

-(void)chooseConcern{
    [self commonAlertWithContent:@"社会关注程度" choosenBlock:^(PropertyDictItemModel *model) {
        self.concernModel = model;
        self.concernStateField.text = model.displayName;
    } getList:^(NSArray *list) {
        self.concerns = list;
    }];
}

-(void)chooseState{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.stateModel.ids = @"1";
        self.stateField.text = @"存在";
    }];
    [alert addAction:action];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.stateModel.ids = @"0";
        self.stateField.text = @"不存在";
    }];
    [alert addAction:action1];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:cancel];
    
    [self presentVC:alert];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
            [self pickerImage];
            break;
        case 2:
            [self chooseOrgType];
            break;
        case 8:
            [self chooseState];
            break;
        case 11:
            [self chooseConcern];
            break;
        case 16:
            [self chooseManipulatorCardType];
            break;
        default:
            break;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    24,25,26,27
    switch (indexPath.row) {
        case 24:{
            self.dangyuanCell.hidden = !self.havingOrgBtn.selected;
            return self.dangyuanCell;
        }
            break;
        case 25:{
            self.gonghuiCell.hidden = !self.havingWorkingOrgBtn.selected;
            return self.gonghuiCell;
        }
            break;
        case 26:{
            self.femaleCell.hidden = !self.havingFemaleOrgBtn.selected;
            return self.femaleCell;
        }
            break;
        case 27:{
            self.tuanCell.hidden = !self.havingYoungthOrgBtn.selected;
            return self.tuanCell;
        }

        default:
            return [super tableView:tableView cellForRowAtIndexPath:indexPath];
            break;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 24:{
            return self.havingOrgBtn.selected ? 44 : 0;
        }
            break;
        case 25:{
            return self.havingWorkingOrgBtn.selected ? 44 : 0;
        }
            break;
        case 26:{
            return self.havingFemaleOrgBtn.selected ? 44 : 0;
        }
            break;
        case 27:{
            return self.havingYoungthOrgBtn.selected ? 44 : 0;
        }
        default:
            return [super tableView:tableView heightForRowAtIndexPath:indexPath];
            break;
    }
}

@end
